<template>
  <div class="box">
    <div class="w-full h-[220px] lg:h-[300px]">
      <img :src="topImg" alt="" class="w-full h-full object-cover" />
    </div>
    <div class="b p-3 lg:pt-[60px]">
      <div class="flex flex-col gap-5">
        <div class="flex gap-2">
          <span class="w-[110px]">推荐：</span>
          <ul class="flex-1 list flex gap-[20px] flex-wrap">
            <li
              class="list-item"
              :class="{ 'list-active': searchObj.is_hot === null }"
              @click="searchObj.is_hot = null"
            >
              全部
            </li>
            <li
              class="list-item"
              :class="{ 'list-active': searchObj.is_hot === 1 }"
              @click="searchObj.is_hot = 1"
            >
              热门推荐
            </li>
          </ul>
        </div>
        <div class="flex gap-2">
          <span class="w-[110px]">国家和地区：</span>
          <ul class="flex-1 list flex gap-[20px] flex-wrap">
            <li
              class="list-item"
              :class="{ 'list-active': searchObj.project_area_state === null }"
              @click="searchObj.project_area_state = null"
            >
              全部
            </li>
            <li
              class="list-item"
              v-for="item in areaList"
              :key="item.id"
              :class="{
                'list-active': searchObj.project_area_state === item.id,
              }"
              @click="searchObj.project_area_state = item.id"
            >
              {{ item.title }}
            </li>
          </ul>
        </div>
      </div>
      <!-- 项目列表 -->
      <div class="grid lg:grid-cols-5 gap-5 mt-5">
        <div
          class="w-full xk"
          v-for="item in projectList"
          @click="toDetails(item.id)"
        >
          <div class="w-full pb-[60%] relative">
            <img
              class="w-full h-full absolute"
              :src="item.project_image_url_1"
              alt=""
            />
          </div>
          <h1 class="text-[18px] text-[#555] text-center p-[15px]">
            {{ item.project_title }}
          </h1>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from "vue";
import { useRouter } from "vue-router";
import { projectListApi, systemConfigApi } from "@/api";
const router = useRouter();

const projectList = ref([]);
const total = ref(0);
const searchObj = ref({
  page_size: 20,
  page: 1,
  is_hot: null,
  project_area_state: null,
});
const areaList = [
  { id: 1, title: "美洲" },
  { id: 2, title: "欧洲" },
  { id: 3, title: "亚洲" },
  { id: 4, title: "大洋洲" },
  { id: 5, title: "非洲" },
];
const getProjectList = () => {
  projectListApi(searchObj.value).then((res) => {
    console.log(res);
    projectList.value = res.items;
    total.value = res.total_count;
  });
};
const toDetails = (id) => {
  router.push({
    path: "/projectDetails",
    query: {
      id,
    },
  });
};

watch(
  () => searchObj.value,
  () => {
    searchObj.value.page = 1;
    getProjectList();
  },
  {
    deep: true,
  }
);
const topImg = ref();
onMounted(() => {
  getProjectList();
  systemConfigApi().then((res) => {
    topImg.value = res.project_big_image_url;
  });
});
</script>

<style scoped lang="scss">
.list-item {
  cursor: pointer;
  width: 110px;
  height: 27px;
  font-size: 15px;
  color: #555;
  text-align: center;
  line-height: 27px;
}

.list-active {
  color: #a5772c;
  background: #e4e4e4;
  border-radius: 3px;
}
.box {
  padding-bottom: 100px;

  .list {
  }

  .xk {
    border: 1px solid #e3e3e3;

    &:hover {
      cursor: pointer;
      box-shadow: 0px 0px 16px rgba(85, 85, 85, 0.2);
    }
  }
}
</style>
